/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MainClasses;

import MainClasses.Event.*;
import java.io.*;
import java.util.*;
/**
 *
 * @author andrewcutler
 */
public class XMLParser {
    
    public static void parse(Preferences prefs, CourseManager c, int num)
    {
        try
        {
            File file = new File(prefs.getFileName(num));
            DataInputStream in = new DataInputStream(new FileInputStream(file));
            String currentLine;
            Course currentClass = null;
            Event currentEvent = null;
            boolean[] meets = new boolean[5];
            
            while(in.available() != 0)
            {
                currentLine = in.readLine();
                if(currentLine.contains("<student>"))
                {
                    currentLine = in.readLine();
                    prefs.setStudentName(currentLine.substring(currentLine.indexOf("<name>")+6, currentLine.indexOf("</name>")));
                }
                if(currentLine.contains("</assignments>"))
                {
                    return;
                }
                if(currentLine.contains("</class>"))
                {
                    c.addCourse(currentClass);
                    currentClass = null;
                }
                if(currentLine.contains("<class>"))
                {
                    currentClass = new Course();
                }
                if(currentClass != null)
                {
                    if(currentLine.contains("<id>"))
                    {
                        Integer i = new Integer(currentLine.substring(currentLine.indexOf("<id>")+4, currentLine.indexOf("</id>")));
                        currentClass.setId(i.intValue());
                        ArrayList<Course> courses = c.getCourses();
                        for(Course cc : courses)
                        {
                            if(currentClass.equals(cc))
                            {
                                currentClass = cc;
                            }
                        }
                    }
                    else if(currentLine.contains("<period>"))
                        {
                            String p = currentLine.substring(currentLine.indexOf("<period>")+8, currentLine.indexOf("</period>"));
                            if(p.equals(""))
                            {
                                currentClass.setPeriod(11);
                            }
                            else
                                currentClass.setPeriod(Integer.parseInt(currentLine.substring(currentLine.indexOf("<period>")+8, currentLine.indexOf("</period>"))));
                        }
                        else if(currentLine.contains("<coursename>"))
                        {
                            currentClass.setCourseName(currentLine.substring(currentLine.indexOf("<coursename><![CDATA[") + 21, currentLine.indexOf("]]")));

                        }
                        else if(currentLine.contains("<teacher>"))
                        {
                            currentClass.setTeacher(currentLine.substring(currentLine.indexOf("<teacher>") + 9, currentLine.indexOf("</teacher>")));
                        }
                        else if(currentLine.contains("<courseurl>"))
                        {
                            currentClass.setCourseURL(currentLine.substring(currentLine.indexOf("<courseurl>")+11, currentLine.indexOf("</courseurl>")));
                        }
                        else if(currentLine.contains("<meets>"))
                        {
                            for(int o = 0; o < 3; o++)
                                in.readLine();
                            for(int o = 0; o < 5; o++)
                            {
                                if(in.readLine().contains("true"))
                                {
                                    meets[o] = true;
                                }
                            }
                            currentClass.meets(meets[0], meets[1], meets[2], meets[3], meets[4]);
                        }
                    
                    else if(currentLine.contains("<assignment type="))
                    {
                        if(currentLine.contains("<assignment type=\"hw\">"))
                            currentEvent = new SchoolEvent(Event.HOMEWORK);
                        if(currentLine.contains("<assignment type=\"cw\">"))
                            currentEvent = new SchoolEvent(Event.CLASSWORK);
                        if(currentLine.contains("<assignment type=\"test\">"))
                            currentEvent = new SchoolEvent(Event.CLASSWORK);
                        if(currentLine.contains("<assignment type=\"project\">"))
                            currentEvent = new SchoolEvent(Event.CLASSWORK);
                        
                        ((SchoolEvent)currentEvent).setCourse(currentClass);
                    }
                    else if(currentLine.contains("</assignment>"))
                    {
                        currentClass.addEvent(currentEvent);
                        currentEvent = null;
                    }
                    else if(currentEvent != null)
                    {
                        if(currentLine.contains("<title>"))
                        {
                            currentEvent.setName(currentLine.substring(currentLine.indexOf("<title><!CDATA[")+19, currentLine.indexOf("]]")));
                        }
                        else if(currentLine.contains("<description>"))
                        {
                            currentLine = currentLine.substring(currentLine.indexOf("<description><![CDATA[")+22, currentLine.indexOf("]]"));
                            currentEvent.setDescription(currentLine);
                        }
                        else if(currentLine.contains("<begin>"))
                        {
                            
                            currentLine = currentLine.substring(currentLine.indexOf("<begin>")+7, currentLine.indexOf("</begin>"));
                            
                            //int year = Integer.parseInt(currentLine.substring(0,4));
                            //int month = Integer.parseInt(currentLine.substring(5,7));
                            //int day = Integer.parseInt(currentLine.substring(8));
                            currentEvent.setStartDate(MyDate.parseDate(currentLine, MyDate.FORMAT_3));
                            
                        }
                        else if(currentLine.contains("<end>"))
                        {
                            currentLine = currentLine.substring(currentLine.indexOf("<end>")+5, currentLine.indexOf("</end>"));
                            
                            //int year = Integer.parseInt(currentLine.substring(0,4));
                            //int month = Integer.parseInt(currentLine.substring(5,7));
                            //int day = Integer.parseInt(currentLine.substring(8));
                            currentEvent.setEndDate(MyDate.parseDate(currentLine, MyDate.FORMAT_3));
                        }
                    }
                    
                }
                    
                
            }
            
            in.close();
            
            
                
        }
        catch(IOException e)
        {
            System.out.println("ERROR: " + e);
        }
    }
    
    
    
//    public static void saveAssignments(Preferences prefs, CourseManager manager)
//    {
//        try
//        {
//        System.out.println("is printing");
//        PrintWriter out;
//        
//            out = new PrintWriter(new FileWriter(prefs.getFileName()));
//            String outputString = "";
//            ArrayList<Course> courses= manager.getCourses();
//            outputString = "<assignments>";
//            outputString += "\n";
//            for(int i = 0; i < courses.size(); i++)
//            {
//                
//                Course currentCourse = courses.get(i);
//                outputString += "\t<class>";
//                outputString += "\n";
//                outputString += "\t\t<id>" + currentCourse.getCourseID() + "</id>";
//                outputString += "\n";
//                outputString += "\t\t<period>" + currentCourse.getPeriod() + "</period>";
//                outputString += "\n";
//                outputString += "\t\t<coursename><![CDATA[" + currentCourse.getCourseName() + "]]></coursename>";
//                outputString += "\n";
//                outputString += "\t\t<teacher>" + currentCourse.getTeacher() + "</teacher>";
//                outputString += "\n";
//                outputString += "\t\t<courseurl>" + currentCourse.getCourseURL() + "</courseurl>";
//                outputString += "\n";
//                
//                ArrayList<Assignment> assignments = currentCourse.getAssignments();
//                
//                for(int j = 0; j < assignments.size(); j++)
//                {
//                    Assignment currentAssign = assignments.get(j);
//                    outputString += "\t\t";
//                    if(currentAssign.isHomework())
//                        outputString += "<assignment type=\"hw\">";
//                    if(currentAssign.isClasswork())
//                        outputString += "<assignment type=\"cw\">";
//                    if(currentAssign.isTest())
//                        outputString += "<assignment type=\"test\">";
//                    if(currentAssign.isProject())
//                        outputString += "<assignment type=\"project\">";
//                    
//                    outputString += "\n";
//                    
//                    outputString += "\t\t\t<end>" + currentAssign.getDateDue().toString(MyDate.FORMAT_3, "-") + "</end>";
//                    outputString += "\n";
//                    outputString += "\t\t\t<begin>" + currentAssign.getDateAssigned().toString(MyDate.FORMAT_3, "-") + "</begin>";
//                    outputString += "\n";
//                    
//                    outputString += "\t\t\t<title><![CDATA[  " + currentAssign.getTitle() + "]]></title>";
//                    outputString += "\n";
//                    
//                    outputString += "\t\t\t<description-notags><![CDATA[" + Assignment.removeEscapeSequences(currentAssign.getText()) + "]]></description-notags>";
//                    outputString += "\n";
//                    outputString += "\t\t\t</assignment>";
//                    outputString += "\n";
//                    
//                    
//                }
//                
//                outputString += "\t\t</class>";
//                outputString += "\n";
//            }
//            
//            outputString += "</assignments>";
//            outputString += "\n";
//            out.println(outputString);
//            out.close();
//        }
//        catch(Exception e)
//        {
//            System.out.println(e);
//            System.exit(1);
//        }
//    }

}
